package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;


import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.PopwindowMapCarBinding;
import com.sgb.kjwl.model.entity.transport.CarriageInfo;
import com.sgb.kjwl.utils.DefaultUtil;
import com.sgb.kjwl.utils.NumberUtil;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.utils.VehicleConstant;
import com.sgb.kjwl.view.ui.activity.self.logistics.InquiryActivity;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.dialog.RoundDialog;
import com.im.immodel.IMUtils;

public class MapCarriagePopWindow extends BasePopWindow<Object> implements View.OnClickListener {
    private PopwindowMapCarBinding binding;
    private CarriageInfo mEntity;

    public MapCarriagePopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_map_car;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        binding = (PopwindowMapCarBinding) holder.getmBinding();
        binding.tvMsg.setOnClickListener(this);
        binding.tvTel.setOnClickListener(this);
        binding.tvBtn.setOnClickListener(this);
    }

    /**
     * 设置数据
     */
    public void setData(CarriageInfo entity) {
        mEntity = entity;
        if (mEntity == null) return;
        binding.tvNumber.setText(String.format("%s", entity.getCarNumber()));
        binding.tvDriver.setText(String.format("%s", entity.getName()));
        binding.tvDistance.setText(String.format("%s", Double.valueOf(entity.getDistance()) < 1000
                ? "1km以内"
                : String.format("%skm", NumberUtil.trim0(Double.valueOf(entity.getDistance()) / 1000))));
        binding.tvCarInfo.setText(String.format("%s/%s/%s吨%s%s",
                entity.getCarTypeName(),
                entity.getCarLenName(),
                NumberUtil.trim0(entity.getWeight()),
                TextUtils.isEmpty(entity.getHeightHurdle())
                        ? ""
                        : String.format("/%s", entity.getHeightHurdle()),
                entity.getIsDump() == null
                        ? ""
                        : String.format("/%s", VehicleConstant.getIsDumpStr(entity.getIsDump()))));
        binding.tvIntentionally.setText(String.format("意向地:%s  %s",
                DefaultUtil.getString(entity.getDestCityName()),
                DefaultUtil.getString(entity.getDestDistrictName())));
        binding.tvPriceTip.setText("意向报价:");
        binding.tvPrice.setText(String.format("%s", TextUtils.isEmpty(entity.getFreight())
                ? ""
                : String.format("%s元/吨", NumberUtil.trim0(entity.getFreight()))));
        //无im信息置灰消息按钮
        if (entity.getUserVo() == null || TextUtils.isEmpty(entity.getUserVo().getImAccount())) {
            binding.tvMsg.setAlpha(0.5f);
            binding.tvMsg.setEnabled(false);
        } else {
            binding.tvMsg.setAlpha(1f);
            binding.tvMsg.setEnabled(true);
        }
        //无手机号信息置灰联系按钮
        if (TextUtils.isEmpty(entity.getTelphone())) {
            binding.tvTel.setAlpha(0.5f);
            binding.tvTel.setEnabled(false);
        } else {
            binding.tvTel.setAlpha(1f);
            binding.tvTel.setEnabled(true);
        }
        //resetImMsg(entity);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_msg:
                imClick();
                break;
            case R.id.tv_tel:
                telClick();
                break;
            case R.id.tv_btn:
                inquiryClick();
                break;
        }
    }

    private void imClick() {
        //检查登录状态
        if (UserOperating.getInstance().isNotLogged(mContext)) {
            return;
        }
        if (mEntity != null && mEntity.getUserVo() != null) {
            IMUtils.getInstance().buildSingleTalking(mEntity.getUserVo().getImAccount());
//            MToast.showToast(mContext, "功能暂未实现");
//            NimUIKit.startP2PSession(mContext, mEntity.getUserVo().getImAccount());
          /*  //清除消息提醒
            if (onResultCallBack != null) {
                onResultCallBack.result(mEntity.getImContact());
            }*/
           // resetImMsg(mEntity);
        }
    }

    private void telClick() {
        //检查登录状态
        if (UserOperating.getInstance().isNotLogged(mContext)) {
            return;
        }
        if (mEntity != null && !TextUtils.isEmpty(mEntity.getTelphone())) {
            showTelDialog(mEntity.getTelphone());
        }
    }

    private void inquiryClick() {
        //暂时隐藏询价
        //检查登录状态
        if (UserOperating.getInstance().isNotLogged(mContext)) {
            return;
        }
        //团队认证检查
        if (UserOperating.getInstance().isAuthOwner()) {
            return;
        }
        //询价填写资料页
        InquiryActivity.startCarInquiry(mContext,DefaultUtil.getString(mEntity.getCarrierNo()),mEntity.getTelphone());
       /* //清除消息提醒
        if (onResultCallBack != null) {
            onResultCallBack.result(mEntity.getImContact());
        }*/
    }

/*    *//**
     * 设置im消息红点
     *//*
    private void resetImMsg(CarriageInfo entity) {
        if (entity.getImContact() != null && entity.getImContact().getUnreadCount() > 0) {
            binding.tvMsgBadge.setText(entity.getImContact().getUnreadCount() > 99
                    ? "99+"
                    : String.valueOf(entity.getImContact().getUnreadCount()));
            binding.tvMsgBadge.setVisibility(View.VISIBLE);
        } else {
            binding.tvMsgBadge.setVisibility(View.GONE);
        }
    }*/

    private void showTelDialog(String phone) {
        new RoundDialog.Builder(mContext)
                .title("是否拨打电话")
                .content(phone)
                .confirm("拨打")
                .cancelable(false)
                .confirmCallback(v -> {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + phone);
                    intent.setData(data);
                    mContext.startActivity(intent);
                })
                .show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        clearAlpha();
    }
}
